var path = require('path');

var pkgAdapter = {
    existDir : function(dir){
        dir = (dir.indexOf('./') === 0 || dir.indexOf('.\\') === 0) ? dir.substr(2) : dir;
        let dir2 = './' + dir;
        let dir3 = '.\\' + dir;
        return pkgAdapter.existPath(dir) || pkgAdapter.existPath(dir2) || pkgAdapter.existPath(dir3);
    },

    existPath : function(path){
        let config = require('./bin/getConfig')('package.json');
        if (typeof(config.pkg) !== 'object')
            return false;
        if (typeof(config.pkg.scripts) === 'string'){
            if (config.pkg.scripts.indexOf(path) === 0)
                return true;
        }
        if (config.pkg.scripts instanceof Array){
            for (let scr of config.pkg.scripts){
                if (typeof(scr) === 'string' && scr.indexOf(path) === 0)
                    return true;
            }
        }
        if (typeof(config.pkg.assets) === 'string'){
            if (config.pkg.assets.indexOf(path) === 0)
                return true;
        }
        if (config.pkg.assets instanceof Array){
            for (let ass of config.pkg.assets){
                if (typeof(ass) === 'string' && ass.indexOf(path) === 0)
                    return true;
            }
        }
        return false;
    },

    getFullPath : function(dir){
        return path.join(pkgAdapter.existDir(dir) ? __dirname : process.cwd(), dir);
    }
};

module.exports = pkgAdapter;